#include "checkboxdelegate.hpp"

#include <qcheckbox.h>

CheckBoxDelegate::CheckBoxDelegate(QObject *parent):QStyledItemDelegate(parent)
{

}

QWidget *CheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QCheckBox *editer = new QCheckBox(parent);
    return editer;
}

void CheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{

    QCheckBox* cb = static_cast<QCheckBox*>(editor);
    if(cb)
    {
        QVariant flag = index.data (Qt::CheckStateRole);
        if(flag.isValid ())
        {
            if(flag.toInt () ==Qt::Checked)
            {
                cb->setCheckState(Qt::Unchecked);
            }else
            {
                cb->setCheckState(Qt::Checked);
            }

        }
    }
}

void CheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QCheckBox* cb = static_cast<QCheckBox*>(editor);
   if(cb)
   {
       model->setData (index,cb->checkState (), Qt::CheckStateRole);
   }
}
